package com.shi.yibanjia.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shi.yibanjia.annotation.Auth;
import com.shi.yibanjia.annotation.MyLog;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.entity.po.CarType;
import com.shi.yibanjia.enums.AuthCodeEnum;
import com.shi.yibanjia.enums.LogType;
import com.shi.yibanjia.service.CarTypeService;
import com.shi.yibanjia.util.AliYunOSSUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/car")
public class CarTypeController {

    @Resource
    private CarTypeService carTypeService;

    @Cacheable(value = "yibanjia:car", key = "'list'")
    @GetMapping
    @Auth(hasPerm = AuthCodeEnum.BIZ_CARTYPE_DQL)
    public Result getCarList() {
        return Result.ok(carTypeService.list());
    }


    @Cacheable(value = "yibanjia:car", key = "'id:'+#id")
    @GetMapping("/{id}")
    @Auth(hasPerm = AuthCodeEnum.BIZ_CARTYPE_DQL)
    public Result getCarById(@PathVariable("id") Long id) {
        return Result.ok(carTypeService.getById(id));
    }

    @MyLog(type = LogType.UPDATE)
    @CacheEvict(value = "yibanjia:car", allEntries = true)
    @PutMapping
    @Auth(hasPerm = AuthCodeEnum.BIZ_CARTYPE_DML)
    public Result update(@RequestBody CarType carType) {
        carTypeService.updateById(carType);
        return Result.ok(carType);
    }

    @MyLog(type = LogType.INSERT)
    @CacheEvict(value = "yibanjia:car", allEntries = true)
    @PostMapping
    @Auth(hasPerm = AuthCodeEnum.BIZ_CARTYPE_DML)
    public Result add(@Valid @RequestBody CarType carType) {
        CarType one = carTypeService.getOne(new LambdaQueryWrapper<CarType>()
                .eq(CarType::getName, carType.getName()));
        if (ObjUtil.isNotEmpty(one)) {
            return Result.fail("该汽车类型名称已存在");
        }
        carTypeService.save(carType);
        return Result.ok();
    }

    @MyLog(type = LogType.DELETE)
    @CacheEvict(value = "yibanjia:car", allEntries = true)
    @DeleteMapping("/{id}")
    @Auth(hasPerm = AuthCodeEnum.BIZ_CARTYPE_DML)
    public Result remove(@PathVariable("id") Long id) {
        CarType carType = carTypeService.getById(id);
        carTypeService.removeById(id, false);
        String baseUrl = AliYunOSSUtil.getBaseUrl();
        List<String> list = new ArrayList<>();
        if (StrUtil.isNotBlank(carType.getCoverImage())) {
            list.add(carType.getCoverImage().replace(baseUrl, ""));
        }
        if (StrUtil.isNotBlank(carType.getPicture1())) {
            list.add(carType.getPicture1().replace(baseUrl, ""));
        }
        if (StrUtil.isNotBlank(carType.getPicture2())) {
            list.add(carType.getPicture2().replace(baseUrl, ""));
        }
        if (StrUtil.isNotBlank(carType.getPicture3())) {
            list.add(carType.getPicture3().replace(baseUrl, ""));
        }
        if (CollUtil.isNotEmpty(list)) {
            AliYunOSSUtil.removeBatchObjects(list);
        }
        return Result.ok();
    }
}
